﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using ImageHandler.Models;
using Microsoft.Win32;
using OpenCvSharp;
using System.IO;
using System.Windows.Media.Imaging;

namespace ImageHandler.ViewModels
{
    public class PartViewModel : ObservableObject
    {
        private string _fileName;

        private string header;
        public string Header
        {
            get { return header; }
            set
            {
                header = value;
                OnPropertyChanged("Header");
            }
        }

        public ResultModel Result { get; set; } = new();
        public Action<string> OperateLogAction { get; set; }
        public RelayCommand<string> OperateCommand { get; set; }

        public PartViewModel()
        {
            OperateCommand = new RelayCommand<string>(Execute);
        }

        private void Execute(string action)
        {
            if (action == "OpenFile")
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.DefaultDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assert\\Images");
                openFileDialog.Filter = "Image files (*.jpg; *.jpeg; *.png) |*.jpg;*.jpeg;*.png| All files (*.*) | *.*";
                if (openFileDialog.ShowDialog() == true)
                {
                    // Handle the selected file
                    _fileName = openFileDialog.FileName;
                    var bitmap = new BitmapImage(new Uri(_fileName, UriKind.RelativeOrAbsolute));
                    Result.OriginalImage = bitmap;
                    OperateLogAction?.Invoke($"选择文件 {_fileName}!");
                    Open(_fileName);
                }
            }
            else if (action == "Run")
            {
                if (!string.IsNullOrWhiteSpace(_fileName) && System.IO.File.Exists(_fileName))
                {
                    Run(_fileName);
                }
            }
            else
            {
                Operate(_fileName, action);
            }
        }

        public virtual void Open(string fileName)
        {
            // Initialize any properties or commands here
        }

        public virtual void Run(string fileName) 
        {
        
        }

        public virtual void Operate(string _fileName, string action)
        {

        }

        public void AddMatToResultInfos(Mat mat, string title)
        {
            var reslutInfo = new ReslutInfoModel { Title = title };
            reslutInfo.Image = Mat2BitmapImage(mat);
            Result.ReslutInfos.Add(reslutInfo);
        }

        public void AddMatToResult(Mat mat, string title)
        {
            var reslutInfo = new ReslutInfoModel { Title = title };
            reslutInfo.Image = Mat2BitmapImage(mat);
            Result.SingleResult = reslutInfo;
        }

        /// <summary>
        /// Convert Mat to BitmapImage
        /// </summary>
        /// <param name="mat"></param>
        /// <returns></returns>
        public BitmapImage Mat2BitmapImage(Mat mat)
        {
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = mat.ToMemoryStream(".png");
            bitmap.EndInit();
            return bitmap;
        }

        public override string ToString()
        {
            return Header.ToString();
        }
    }
}
